package cn.wolfcode.web.controller;


import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequiresPermissions(value = {"systemDictionaryItem:list", "字典明细列表"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo) {
        model.addAttribute("pageInfo", systemDictionaryItemService.query(qo));
        model.addAttribute("dics", systemDictionaryService.listAll());
        return "systemDictionaryItem/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"systemDictionaryItem:delete", "字典明细删除"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        if (id != null) {
            systemDictionaryItemService.delete(id);
        }
        return new JSONResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"systemDictionaryItem:saveOrUpdate", "字典明细新增/修改"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if (systemDictionaryItem.getId() != null) {
            systemDictionaryItemService.update(systemDictionaryItem);
        } else {
            systemDictionaryItemService.save(systemDictionaryItem);
        }
        return new JSONResult();
    }
}
